package cn.tedu.interceptor.config;

import cn.tedu.interceptor.Interceptor.MyInterceptor;
import cn.tedu.interceptor.Interceptor.MyInterceptor1;
import cn.tedu.interceptor.Interceptor.MyInterceptor2;
import cn.tedu.interceptor.Interceptor.MyInterceptor3;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class Config implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //将我们定义的拦截器MyInterceptor注册到SpringMVC框架中
        //当SpringMVC启动时(对应的starter)就会将当前拦截器利用起来
        //如果拦截器注册时不指定路径,则是所有路径都需要经过拦截器
        /*
            http://localhost:8080/user/reg          拦截,其他不拦截
            http://localhost:8080/user/login
            http://localhost:8080/user/index.html
            http://localhost:8080/user/123.html
         */
//        registry.addInterceptor(new MyInterceptor());

        //可以在注册拦截器时添加拦截路径,那么仅当该路径被请求时才会使用当前拦截器
//        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/users/reg");


        /*
            http://localhost:8080/user/reg          拦截
            http://localhost:8080/user/login        不拦截
            http://localhost:8080/user/index.html   拦截
            http://localhost:8080/user/123.html     不拦截
         */
        //可以指定多个拦截路径,那么只有这些路径被请求时才会进行拦截
//        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/users/reg","/index.html");

        //http://localhost:8080/users/index.html   /users/...都拦截,其他不拦截


//        registry.addInterceptor(new MyInterceptor())
//                .addPathPatterns("/users/**")///users的都拦截
//                .excludePathPatterns("/users/login");//被排除在外的路径

        registry.addInterceptor(new MyInterceptor1());
        registry.addInterceptor(new MyInterceptor2());
        registry.addInterceptor(new MyInterceptor3());
    }
}
